package com.ld.xuanKe.controller;

import com.alibaba.excel.EasyExcel;
import com.ld.xuanKe.entity.Course;
import com.ld.xuanKe.entity.MySelect;
import com.ld.xuanKe.entity.Selectd;
import com.ld.xuanKe.entity.User;
import com.ld.xuanKe.listener.MyUploadUserDataListener;
import com.ld.xuanKe.service.SelectdService;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName xuanKe
 * @ClassName SelectAction
 * @Author 刘邓
 * @Date 2023-12-15 10:42
 */
@Controller
@Scope("prototype")
public class SelectdAction implements ModelDriven<Selectd> {
	private Selectd selectd;
	@Resource
	private SelectdService selectdService;
	//excel模板下载
	private InputStream inputStream;
	private String filename;
	//导入excel
	private File uploadFile;
	private String uploadFileFileName;

	public Selectd getModel() {
		selectd = new Selectd();
		return selectd;
	}
	public String add(){
		Timestamp current = Timestamp.valueOf(LocalDateTime.now());
		selectd.setTime(current);
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		selectd.setUserId(user.getId());
		int add = selectdService.add(selectd);
		if (add == -2) {
			session.setAttribute("message", "该课程名已被选择");
		}
		if (add == -3) {
			session.setAttribute("message", "该学生学分已选满35");
		}
		if (add == 1) {
			session.setAttribute("message", "选课成功");
		} else {
			session.setAttribute("message", "选课失败");
		}
		return "selectd-add";

	}
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		int delete = selectdService.delete(selectd.getId());
		if (delete == 1) {
			session.setAttribute("message", "退课成功");
		} else {
			session.setAttribute("message", "退课失败");
		}
		return "selectd-delete";
	}
	//导出excel
	public void downLoadMyCourses() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			HttpServletResponse resp = ServletActionContext.getResponse();
			User user = (User)session.getAttribute("user");
			List<MySelect> mySelects = new ArrayList<MySelect>();
			List<Selectd> selectds = new ArrayList<Selectd>();
			if(user.getStatus() == 0){
				 mySelects = selectdService.showMyCourse(user.getId());
			}else {
				 selectds =  selectdService.list();
			}
			resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			resp.setCharacterEncoding("utf-8");
			// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
			String fileName = URLEncoder.encode("课程信息", "UTF-8").replaceAll("\\+", "%20");
			resp.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
			if(user.getStatus() == 0){
				EasyExcel.write(resp.getOutputStream(), MySelect.class).sheet("选课信息").doWrite(mySelects);
			}else {
				EasyExcel.write(resp.getOutputStream(), Selectd.class).sheet("选课信息").doWrite(selectds);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	//	模板下载
	public String excelTemplate() {
		return "success";
	}
	public InputStream getInputStream() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			String filePath = "/home/upload/xuanKeTemplate.xlsx";
			inputStream = new FileInputStream(ServletActionContext.getRequest().getServletContext().getRealPath(filePath));
			return inputStream;
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	//导入excel
	public String importExcel() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			if (uploadFile == null) {
				session.setAttribute("message", "导入失败");
				return "selectd-import";
			}
			EasyExcel.read(new FileInputStream(uploadFile), Selectd.class, new MyUploadUserDataListener()).sheet().doRead();
			session.setAttribute("message", "导入成功");
			return "selectd-import";
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
}
